package com.chub.study.modules.controller;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.chub.RespResult;
import com.chub.study.modules.entity.TAddrInfo;
import com.chub.study.modules.service.TAddrInfoService;
import com.chub.study.util.SmartWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;


/**
 * <p>
 * 地址信息表
 * </p>
 *
 * @author Chub
 * @since 2023-06-08
 */
@Api(tags = "地址信息表服务")
@RestController
@RequestMapping("/taddrinfo")
public class TAddrInfoController {

    @Resource
    private TAddrInfoService tAddrInfoService;

    /**
     * 创建地址信息
     *
     * @param tAddrInfo 待创建的地址信息
     * @return 返回操作结果
     */
    @ApiOperation("创建")
    @PostMapping("/create")
    public RespResult<String> create(@Valid @RequestBody TAddrInfo tAddrInfo) {
        tAddrInfoService.save(tAddrInfo);
        return RespResult.success();
    }

    @ApiOperation(value = "删除")
    @PostMapping("/delete")
    public RespResult<String> delete(@RequestParam String id) {
        tAddrInfoService.removeById(id);
        return RespResult.success();
    }

    @ApiOperation(value = "修改")
    @PostMapping("/modify")
    public RespResult<String> modify(@Valid @RequestBody TAddrInfo tAddrInfo) {
        tAddrInfoService.updateById(tAddrInfo);
        return RespResult.success();
    }

    @ApiOperation(value = "查询详情")
    @GetMapping("/detail")
    public RespResult<TAddrInfo> detail(@RequestParam String id) {
        return RespResult.success(tAddrInfoService.getById(id));
    }

    @ApiOperation(value = "分页查询")
    @PostMapping("/queryByPage")
    public RespResult<IPage<TAddrInfo>> queryByPage(@RequestBody JSONObject queryObject) {

         SmartWrapper<TAddrInfo> smartWrapper = new SmartWrapper<>();
         smartWrapper.parse(queryObject, TAddrInfo.class);

         QueryWrapper<TAddrInfo> queryWrapper = smartWrapper.getQueryWrapper();
         IPage<TAddrInfo> page = smartWrapper.generatePage();
         tAddrInfoService.page(page, queryWrapper);
         return RespResult.success(page);
    }

}